function websocketQrcode() {
  this.fetchQrcodeTimer = null
  const isSupportWebSocket = !!window.WebSocket && window.WebSocket.prototype.send;
  const SOCKET_HOST = this.getSocketHost();
  const self = this
  if (isSupportWebSocket) {
    const socketProtocol = location.protocol === "https:" ? "wss://" : "wss://";
    const socketObj = new WebSocket(`${socketProtocol}${SOCKET_HOST}/wsapp/`);
    // 调用者自定义事件回调函数： onclose
    socketObj.onclose = function(e) {
      self.isFunction(this.onclose) && this.onclose(e);
    }
    socketObj.onopen = function(onopenEvent) {
      socketObj.onmessage = function (event) {
        const msg = JSON.parse(event.data);
        // 调用者自定义事件回调函数： onticket
        //获取二维码图片
        if(!!msg.ticket) {
          self.isFunction(self.onticket) && self.onticket(msg.ticket, msg.qrcode ? msg.qrcode : undefined);
        }
        if (msg.op === 'requestlogin') {
          // const expire = (msg.expire_seconds || 60) * 1e3;
          // 60秒二维码过期，50秒刷新一次，防止获取二维码过程中就二维码已经过期，新的还没替换到页面
          self.fetchQrcodeTimer = setTimeout(function(){
            clearTimeout(self.fetchQrcodeTimer);
            self.fetchQrcode(socketObj);
          }, 5e4)
        }
        //扫描登录成功
        if (msg.op === 'loginsuccess') {
          const UserID = msg.UserID;
          const Auth = msg.Auth;
          // 调用者自定义事件回调函数： onloginsuccess
          // 扫码成功后触发 onloginsuccess 事件
          self.isFunction(self.onloginsuccess) && self.onloginsuccess({
            UserID,
            Auth
          });

          self.fetchQrcodeTimer && clearTimeout(self.fetchQrcodeTimer)
        }
      };
      self.fetchQrcode(socketObj);
    }
  }
}

websocketQrcode.prototype = {
  getSocketHost() {
    // return process.env.NODE_ENV === 'production' ? location.host : 'b.yuketang.cn'
    return process.env.NODE_ENV === 'production' ? location.host : 'pro.xuetangonline.com'
  },
  fetchQrcode(socketObj) {
    socketObj instanceof WebSocket && socketObj.send(JSON.stringify({
      'op':"requestlogin",
      'role': "web",
      "version": 1.4,
      "type": "qrcode"
    }));
  },
  isFunction(fn) {
    return typeof fn === "function"
  },
  // 扫码成功，清理定时器，防止再次请求二维码
  successAuth() {
    console.log(this.fetchQrcodeTimer, '二维码')
    this.fetchQrcodeTimer && clearTimeout(this.fetchQrcodeTimer)
  }
};

export default websocketQrcode